package com.work.shart.user.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.work.shart.common.exception.ExceptionEnum;
import com.work.shart.common.mybatis.service.impl.SuperServiceImpl;
import com.work.shart.common.result.ResultVo;
import com.work.shart.common.token.JwtTokenUtil;
import com.work.shart.common.token.UserToken;
import com.work.shart.user.entity.SysUserEntity;
import com.work.shart.user.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Random;

import static com.work.shart.common.result.Result.error;
import static com.work.shart.common.result.Result.success;

/**
 * <p>
 * 用户 前端控制器
 *
 * @author cong
 * </p>
 * @author cong
 * @since 2022-03-30
 */
@RestController
@Api(tags = "登录")
@RequestMapping("/api/login")
public class LoginController extends SuperServiceImpl<ISysUserService, SysUserEntity> {


    @ApiOperation("授权")
    @PostMapping("token")
    public ResultVo token(@RequestBody SysUserEntity entity) {
        QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", entity.getAccount());
        queryWrapper.eq("password", entity.getPassword());
        SysUserEntity sysUserEntity = getBaseService().getOne(queryWrapper);
        if (sysUserEntity != null) {
            return success(JwtTokenUtil.createToken(JSONObject.toJSONString(BeanUtil.copyProperties(entity, UserToken.class))));
        }
        return error(ExceptionEnum.LOGIN_ERROR.getCode(), ExceptionEnum.LOGIN_ERROR.getMsg());
    }


    @ApiOperation("发送短信")
    @GetMapping("sendSign")
    public ResultVo send(@RequestParam("iphone") String iphone) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "LTAISNeyoKobJ1R8", "eedJK9ofxmqcAKQQJhwrBJYQqqLuCw");
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(iphone);//接收短信的手机号码
        request.setSignName("资源分享平台");//短信签名名称
        request.setTemplateCode("SMS_238150475");//短信模板CODE
        Random random=new Random();
        Map<String,String>map= Maps.newHashMap();
        int rannum= (int)(random.nextDouble()*(99999-10000 + 1))+ 10000;
        map.put("code",String.valueOf(rannum));
        request.setTemplateParam(JSONObject.toJSONString(map));//短信模板变量对应的实际值
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return success();

    }
}
